All objects in the program have an object name that uniquely identifies them in a given document.
This information applies to all objects derived from App DocumentObject (App::DocumentObject
class), which essentially comprises all objects that are possible to create in a document.
There are various properties for Names:
Name
can only include simple alphanumeric characters, and the underscore, [_0-9a-zA-Z]
.Name
cannot start with a number; it must start with a letter or the underscore, [_a-zA-Z]
.Name
is assigned at the creation time of the object; afterwards it is no longer editable. The object can never be renamed.Name
must be unique in the entire document. It doesn't matter if two objects are of completely different types, for example, one is a PartDesign Pocket and the other is an Arch Wall. They must have different names.Box
, Box001
, Box002
, etc. This prevents naming collision.Name
becomes available to be used by a newly created object. This means that if Box
, Box001
, and Box002
exist, and we delete the first item, the next box created with Part Box will not be Box003
, it will be Box
again, because this string is available to be used once more. Notice that it is not possible to rename Box001
or Box002
to Box
since their names are fixed.In summary, the Name
essentially acts like a unique identifier (UID) for an object. Since a unique Name
is very restrictive, all objects also have a Label
property which allows "renaming" the object to something more descriptive. The internal Name
actually remains fixed, but the user editable Label
can be used in most situations where the Name
would be used. In common usage in the program and the documentation, "renaming" means changing the Label
and not the actual Name
of the object.
There are various properties for Labels:
Label
can accept any UTF8 string, including accents and spaces.Label
of the object, not the Name
. Therefore, whenever a new object is created, it is a good practice to change the Label
to a more descriptive string. To rename (relabel) the object, select it in the tree view and press F2 (or rather Return on macOS), or open the context menu (right-click) and choose Rename.Name
will still be reported in many places, for example, in the status bar or in the selection view, when the object is selected.Name
, many dialogs will display the Name
first, followed by the user editable Label
in parentheses, for example, Box (Extruded piece)
.Label
is unique, just like the Name
. However, this behavior can be changed in the preferences editor, Edit → Preferences → General → Document → Allow duplicate object labels in one document. This means that in general the Label
is not unique in the document, and may actually be repeated. However, the recommendation is to keep the Label
unique, as this is probably what is most useful to identify different objects. When writing custom functions that manipulate objects, the methods should use the Name
of the object rather than its Label
to guarantee that the correct object is used.<<Custom Label With Spaces>>.Height
<<Label may use UTF8 characters>>.Width
It is a simple string that can contain arbitrary text, and therefore can be used for documenting (describing with more detail) the created object.
Label2
attribute from the Python console.See also: FreeCAD Scripting Basics, and scripted objects.
Any object in the software is internally created with the addObject()
method of the document. The majority of 2D and 3D objects that the user will see in the 3D view are derived from a Part Feature. In the following example, the object created is a Part Box.
import FreeCAD as App
doc = App.newDocument()
obj = doc.addObject("Part::Box", "Name")
obj.Label = "Custom label"
The addObject
function has two basic string arguments.
"Part::Box"
.Name
attribute. If it is not provided, it defaults to the same name as the class of the object, that is, "Part__Box"
, where the two invalid symbols, the colons ::
, are replaced by two underscores __
.
Name
can only include simple alphanumeric characters, and the underscore, [_0-9a-zA-Z]
. If other symbols are given, these will be converted to underscores; for example, "A+B:C*"
is converted to "A_B_C_"
.Name
cannot start with a number; it must start with a letter or the underscore, [_a-zA-Z]
. For example, "123ABC"
is converted to "_23ABC"
.Name
is fixed at creation time; it cannot be modified afterwards.Name
must be unique in the entire document. If the same "Name"
is used, a sequential number will be appended automatically so that the resulting names are unique; for example, if "Name"
already exists, then new objects will be called "Name001"
, "Name002"
, "Name003"
, etc.The Label
is a property of the created object and can be changed to a more meaningful text.
Label
is the same as the Name
.Name
, the Label
can accept any UTF8 string, including accents and spaces.Label
can be changed at any point in time just by assigning the desired string, obj.Label = "New label"
All objects in a document are data attributes of the corresponding Document object. The attribute's name correspond to the internal Name
of the object.
import FreeCAD as App
obj1 = App.ActiveDocument.Box
obj2 = App.ActiveDocument.Box001
obj3 = App.ActiveDocument.Box002
This is equivalent to using the getObject
method of the Document.
import FreeCAD as App
obj1 = App.ActiveDocument.getObject('Box')
obj2 = App.ActiveDocument.getObject('Box001')
obj3 = App.ActiveDocument.getObject('Box002')
However, it is also possible to get the object by the more descriptive Label
.
import FreeCAD as App
obj1 = App.ActiveDocument.getObjectsByLabel("Concrete wall")[0]
obj2 = App.ActiveDocument.getObjectsByLabel("Custom parallelepiped")[0]
obj3 = App.ActiveDocument.getObjectsByLabel("Some special name for this cube__002")[0]
Given that the Label
is in general not unique, the getObjectsByLabel
method returns a list with all objects found with that Label
. However, if the Label
is unique in the document then the first element in that list should be the desired object.